package com.hm.recycle.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.hm.common.model.PageInfo;
import com.hm.common.util.DbDocumentDef;
import com.hm.recycle.mongo.domain.DbDocumentNewRecycle;
import com.hm.recycle.mongo.domain.QDbDocumentNewRecycle;
import com.hm.recycle.mongo.vo.NewRecycleVo;
import com.hm.recycle.share.repository.DbDocumentNewRecycleRepository;
import com.hm.recycle.share.service.DbDocumentNewRecycleService;

/**
 * @author shishun.wang
 * @date 下午4:57:46 2016年5月30日
 * @version 1.0
 * @describe
 */
@Service
@SuppressWarnings({ "rawtypes", "unchecked" })
public class DbDocumentNewRecycleServiceImpl implements DbDocumentNewRecycleService {

	@Autowired
	private DbDocumentNewRecycleRepository dbDocumentNewRecycleRepository;

	@Override
	public String addDbDocumentNewRecycle(DbDocumentNewRecycle dbDocumentNewRecycle) {
		return dbDocumentNewRecycleRepository.save(dbDocumentNewRecycle).getId();
	}

	@Override
	public PageInfo<NewRecycleVo> queryNewRecycle(DbDocumentDef documentDef,
			String operationUser, Integer version, Long startTime,
			Long endTime, Integer pageNumber, Integer pageSize) {
		PageInfo<NewRecycleVo> pageInfo = new PageInfo<NewRecycleVo>(pageNumber, pageSize);
		
		QDbDocumentNewRecycle dbDocumentNewRecycle = QDbDocumentNewRecycle.dbDocumentNewRecycle;
		Pageable pageable = new QPageRequest(pageInfo.getPageNumber(), pageInfo.getPageSize(),
				dbDocumentNewRecycle.createTime.desc());
		
		List<NewRecycleVo> newRecycleVos = new ArrayList<NewRecycleVo>();

		Page page = dbDocumentNewRecycleRepository.findAll(pageable);
		List<DbDocumentNewRecycle> content = page.getContent();
		for (DbDocumentNewRecycle newRecycle : content) {
			NewRecycleVo newRecycleVo = new NewRecycleVo();
			{
				newRecycleVo.setId(newRecycle.getId());
				newRecycleVo.setDocumentDef(newRecycle.getDocumentDef());
				newRecycleVo.setTargetContent(newRecycle.getTargetContent());
			}
			
			newRecycleVos.add(newRecycleVo);
		}
		return pageInfo.execute(page, newRecycleVos);
	}

	
}
